

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * @author feng
 * @Date 2021/12/4 11:00
 * 函数式接口
 */
public class LambdaTest {
    // Lambda表达式: 1.无参、无返回值
    @Test
    public void test() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.printf("给钱");
            }
        };
        r1.run();
        System.out.print("88888888888888");
        Runnable r2 = () -> System.out.printf("妈的，给钱");
        r2.run();
    }

    //2.一个参数，没有返回值
    @Test
    public void Test2() {
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.printf(s);
            }
        };
        con.accept("人人为我");
        System.out.println("************");
        Consumer<String> con1 = (String s) -> System.out.println(s);
        con1.accept("我为人人");
    }

    // 3.类型可以省略
    @Test
    public void Test3() {
        Consumer<String> con1 = (s) -> System.out.println(s);
        con1.accept("Hello");
    }

    // 4.当只有一个参数时,类型可以省略
    @Test
    public void Test4() {
        Consumer<String> con1 = s -> System.out.println(s);
        con1.accept("Hello,everyone");
    }

    // 4.多个参数，并且有返回值
    @Test
    public void Test5() {
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };
        System.out.println(com1.compare(20, 20));
        System.out.println("********************");
        Comparator<Integer> com2 = (o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        System.out.println(com2.compare(21, 20));
    }

    // 6.当表达式只有一条语句时，返回值、大括号都可以省略；
    @Test
    public void Test6() {
        Comparator<Integer> com2 = (o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        System.out.println(com2.compare(21, 20));
        System.out.println("*****************");

        Comparator<Integer> com3 = (o1, o2) -> o1.compareTo(o2);
        System.out.println(com3.compare(15, 20));
    }

}


